package com.cskaoyan.controller.system;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.vo.MarketAdminVO;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.utils.PasswordGenerateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: Yang Yi-zhou
 * @Date: 2022/9/6 19:29
 * @Description:
 */
//@RequiresPermissions("aaa")
@RequestMapping("admin/admin")
@RestController
@Cacheable(value = "admin::admin",keyGenerator = "customKeyGenerator")
public class AdminController {

    @Autowired
    AdminService adminService;


    @RequiresPermissions("GET /admin/admin/list")
    @RequestMapping("list")
    public BaseRespVo listAdmin(Integer page, Integer limit, String sort, String order) {
        CommonData<MarketAdminVO> data = adminService.listAdmin(page, limit, sort, order);
//        System.out.println("本次查询，查询了数据库");
        return BaseRespVo.ok(data);
    }

    @CacheEvict(value = "admin::admin", allEntries = true)
    @RequestMapping("create")
    public BaseRespVo createAdmin(@RequestBody MarketAdmin marketAdmin) {
        String password = marketAdmin.getPassword();
        String hashedPassword = PasswordGenerateUtil.getHashedPassword(password);
        marketAdmin.setPassword(hashedPassword);
        MarketAdmin data = adminService.createAdmin(marketAdmin);
        return BaseRespVo.ok(data);
    }

    @CacheEvict(value = "admin::admin", allEntries = true)
    @RequestMapping("update")
    public BaseRespVo updateAdmin(@RequestBody MarketAdmin marketAdmin) {
        String password = marketAdmin.getPassword();
        String hashedPassword = PasswordGenerateUtil.getHashedPassword(password);
        marketAdmin.setPassword(hashedPassword);
        MarketAdmin data = adminService.updateAdmin(marketAdmin);
        return BaseRespVo.ok(data);
    }

    @CacheEvict(value = "admin::admin", allEntries = true)
    @RequestMapping("delete")
    public BaseRespVo deleteAdmin(@RequestBody MarketAdmin marketAdmin) {
        Integer rows = adminService.deleteAdmin(marketAdmin);
        if (rows > 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData("删除失败");
    }


}
